package com.jp.knowledge.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.ImageActivity;
import com.jp.knowledge.activity.PhotoActivity;
import com.jp.knowledge.activity.UiImagePreviewActivity;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.ProductUIModel;
import com.jp.knowledge.my.adapter.UiFragmentAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.model.CollectionUiModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/1/23.
 */

public class SearchDetailUiFragment extends SearchDetailFragment implements UiFragmentAdapter.CollectionCallback {

    private UiFragmentAdapter adapter;
    private List<ProductUIModel> uiModels;
    private ArrayList<String> medias;
    private StaggeredGridLayoutManager gridLayoutManager;

    @Override
    protected void initData() {
        super.initData();

        mView.setBackgroundColor(getResources().getColor(R.color.gray_bg));
        uiModels = new ArrayList<>();
        medias = new ArrayList<>();
        gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        gridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                gridLayoutManager.invalidateSpanAssignments();
            }
        });

        adapter = new UiFragmentAdapter(mContext, new ArrayList<ProductUIModel>());
        recyclerView.setAdapter(adapter);
        adapter.setCollectionCallback(this);

        adapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
//                UiImagePreviewActivity.startActivity(mContext, position, medias);
//                Intent intent =new Intent(mContext, PhotoActivity.class);

                ImageActivity.startActivity(mContext, position, -1, uiModels);
            }
        });
    }

    // 搜索结束
    @Override
    public void done(IModel dataModel) {
        super.done(dataModel);
        List<ProductUIModel> models = dataModel.getList(ProductUIModel.class);
        if(uiModels == null)
            uiModels = new ArrayList<>();
        if(isFresh) {
            uiModels.clear();
            footerView.setLoadComplete(false);
        } else if(models == null || models.size() == 0){
            footerView.setLoadComplete(true);
        }
        uiModels.addAll(models);
        medias.clear();
        for(CollectionUiModel model : uiModels){
            medias.add(model.getCover()+"?imageView2/1/interlace/1/format/jpg");
        }
        adapter.updateAdapter(uiModels);
    }

    @Override
    public void collectionOperateSuccess(int position) {}
    @Override
    public void collectionOperateError(int position) {
        ToasUtil.toast(mContext, "抱歉，操作失败，请稍后重试");
    }
}
